﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using DataAccess.Repository;

namespace KPI
{
	public partial class HistoricalData : BasePage
	{
		private IGroupRepository _GroupRepository;

		public int TheGroupID
		{
			get
			{
				return CommonHelper.QueryStringInt("groupid");
			}
		}

		public int CurrentUserGroupID
		{
			get
			{
				var result = 0;

				if(TheCurrentLoginUser != null)
				{
					result = TheCurrentLoginUser.DeptID;
				}

				return result;
			}
		}

		public int TheYear
		{
			get
			{
				return CommonHelper.QueryStringInt("y");
			}
		}

		protected void Page_Init(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
				if(TheCurrentLoginUser == null)
				{
					Response.Redirect("~/Login.aspx");
				}

				if(TheCurrentLoginUser.RoleName.Equals("visitors"))
				{
					Response.Redirect("~/NoPermission.aspx");
				}

				//if(TheGroupID == 0 && CurrentUserGroupID > 0)
				//{
				//  Server.Transfer("~/KPIList.aspx?groupid=" + CurrentUserGroupID.ToString());
				//}
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
				BindYear();

				groupNav.NavPage = "~/HistoricalData.aspx?y=" + TheYear.ToString();

				//var u = KPISiteContext.Current.CurrentUser;

				if(!(TheCurrentLoginUser.RoleName.Equals("administrators") || (TheCurrentLoginUser.RoleName.Equals("unitadministrators") && TheCurrentLoginUser.DeptID == TheGroupID)))
				{
					DisableCopyAndContextMenu();
				}
			}
		}

		protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
		{
			Server.Transfer("~/HistoricalData.aspx?y=" + ddlYear.SelectedValue + "&groupid=" + TheGroupID.ToString());
		}

		protected void Page_PreRender(object sender, EventArgs e)
		{
			BindData();
		}

		protected void rptGroupKPIs_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			var data = e.Item.DataItem as DataAccess.Models.Group;

			if(data != null)
			{
				var TheGroupKPIs1 = e.Item.FindControl("theGroupKPIs1") as Controls.TheGroupKPIs;

				TheGroupKPIs1.TheYear = TheYear;
				TheGroupKPIs1.TheGroup = data;
			}
		}

		protected void Page_UnLoad(object sender, EventArgs e)
		{
			if(_GroupRepository != null)
			{
				((IDisposable)_GroupRepository).Dispose();
			}
		}

		private void BindYear()
		{
			if(ddlYear.Items == null || ddlYear.Items.Count == 0)
			{
				ddlYear.Items.Add(new ListItem("请选择年份", "0"));

				var thisYear = DateTime.Now.Year;
				var startYear = 2012;

				while(startYear < thisYear)
				{
					var item = new ListItem(startYear.ToString(), startYear.ToString());

					if(TheYear == startYear)
					{
						item.Selected = true;
					}

					ddlYear.Items.Add(item);

					startYear++;
				}
			}
		}

		private void BindData()
		{
			if(_GroupRepository == null)
			{
				_GroupRepository = new GroupRepository();
			}

			List<DataAccess.Models.Group> lstDataSource = null;

			if(TheGroupID > 0)
			{
				lstDataSource = new List<DataAccess.Models.Group>();

				var theGroup = _GroupRepository.GetTheGroup(TheGroupID);

				if(theGroup != null)
				{
					lstDataSource.Add(theGroup);
				}
			}
			else
			{
				lstDataSource = _GroupRepository.GetAllGroupWithKPIs(TheYear);
			}

			rptGroupKPIs.DataSource = lstDataSource;
			rptGroupKPIs.DataBind();
		}
	}
}